#include "m_key.h"
#include "STC89C5xRC.H"
#include "lcd1602.h"
#include "task.h"

#if M_KEY_TASK

/* define user matrix port and panel start */
#define MATRIX_KEY_PORT (P3)
static m_key_t m_key_panel = {0, 0};
/* define user matrix port and panel end */

/* define matrix key test count start */
static uint8_t matrix_key_test_count = 0;
/* define matrix key test count end */

/**
 * @brief m_key_get_num
 *
 * @param m_key_t
 */
static void matrix_key_get_num(m_key_t *m_key_param)
{
    switch (m_key_param->value_now)
    {
    case 0xEE:
        m_key_param->value_num = 1;
        break;
    case 0xDE:
        m_key_param->value_num = 2;
        break;
    case 0xBE:
        m_key_param->value_num = 3;
        break;
    case 0x7E:
        m_key_param->value_num = 4;
        break;

    case 0xED:
        m_key_param->value_num = 5;
        break;
    case 0xDD:
        m_key_param->value_num = 6;
        break;
    case 0xBD:
        m_key_param->value_num = 7;
        break;
    case 0x7D:
        m_key_param->value_num = 8;
        break;

    case 0xEB:
        m_key_param->value_num = 9;
        break;
    case 0xDB:
        m_key_param->value_num = 10;
        break;
    case 0xBB:
        m_key_param->value_num = 11;
        break;
    case 0x7B:
        m_key_param->value_num = 12;
        break;

    case 0xE7:
        m_key_param->value_num = 13;
        break;
    case 0xD7:
        m_key_param->value_num = 14;
        break;
    case 0xB7:
        m_key_param->value_num = 15;
        break;
    case 0x77:
        m_key_param->value_num = 16;
        break;

    default:
        break;
    }
}

/**
 * @brief m_key_scan
 *
 * @param m_key_t
 */
void matrix_key_scan(void)
{
    uint8_t m_row, m_col;

    MATRIX_KEY_PORT = 0x0F;

    m_col = MATRIX_KEY_PORT & 0x0F;

    if (m_col != 0x0F)
    {
        delay_ms(1);
        if ((MATRIX_KEY_PORT & 0x0F) != 0x0F)
        {
            m_col = MATRIX_KEY_PORT & 0x0F;
            MATRIX_KEY_PORT = (m_col | 0xF0);
            m_row = (MATRIX_KEY_PORT & 0xF0);
            while ((MATRIX_KEY_PORT & 0xF0) != 0xF0)
                ;
            m_key_panel.value_now = m_col + m_row;
            matrix_key_get_num(&m_key_panel);
        }
    }
    else
    {
        m_key_panel.value_num = 0;
    }
}

/**
 * @brief matrix_struct_panel
 *
 * @return *m_key_t
 */
m_key_t *matrix_struct_panel(void)
{
    return &m_key_panel;
}

/**
 * @brief m_key_scan_proc
 *
 */
void matrix_key_scan_demo(void)
{
    char dis_matrix_str[16];

    /* scan matrix key start */
    matrix_key_scan();
    /* scan matrix key end */

    /* check matrix key value start */
    if (matrix_struct_panel()->value_num != 0)
    {
        /* handle matrix key event start */
        matrix_key_test_count++;
        sprintf(dis_matrix_str, "num:%bu-%bu", matrix_struct_panel()->value_num, matrix_key_test_count);
        lcd1602_clear();
        lcd1602_display_str(0, 0, dis_matrix_str);
        /* handle matrix key event end */
    }
    /* check matrix key value end */
}

#endif